//获取当前用户的购物车信息
loadCart();

// 全选
$('#checkall').click(function () {
    if (this.checked) {
        //全选
        $('.cart-list input[type="checkbox"]').prop('checked', true);
    } else {
        //全不选
        $('.cart-list input[type="checkbox"]').prop('checked', false);
    }
    //计算
    showSelectedCount();
});

//选择按钮的事件
$('.cart-list').on('click', 'input[type="checkbox"]', function () {

//计算
    showSelectedCount();
});
//事件
//减号事件
$('.cart-list').on('click', '.count-minus', function () {
    //获取当前被点击的购物车ID
    var id = $(this).parents('.goods').find('.cart1').attr('data-id');
    var count = $(this).parents('.goods').find('.count-input').text();

    //保证数量最小为1
    (--count <= 0) && (count = 1);

    editCart(id, count);
    console.log(count)
});

//加号事件
$('.cart-list').on('click', '.count-plus', function () {
    //获取当前被点击的购物车ID
    var id = $(this).parents('.goods').find('.cart1').attr('data-id');
    var count = $(this).parents('.goods').find('.count-input').text();
    count++;

    editCart(id, count);
});

$('.cart-list').on('click', '.del', function () {
    var id = $(this).parents('.goods').find('.cart1').attr('data-id');
    delCart(id);
});

//结算按钮
$('.ui-btn-theme').click(()=>{
   //得到被选中的ID
   var checkboxes = $('.cart-list input[type="checkbox"]:checked');
   if (checkboxes.length == 0){
       alert("请至少选中一个商品！！");
   }else {
       var url = "/user/order/settle?";
       checkboxes.each((index,item)=>{
           var cartId = $(item).attr('data-id');
           url+='cartId='+cartId+'&';
       });
       //同步
       window.location.href=url;

   }
});

function showSelectedCount() {
    //计算全选按钮是否被选中
    if ($('.cart-list input[type="checkbox"]').length == $('.cart-list input[type="checkbox"]:checked').length) {
        //全选
        $('#checkall').prop('checked', true);
    } else {
        //没有全选
        $('#checkall').prop('checked', false);
    }
    var count = $('.cart-list input[type="checkbox"]:checked').length;
    $('.count #selectedNum').text(count);
    //计算所有商品的总价
    var sum = 0;
    $('.cart-list input[type="checkbox"]:checked').each(function (index,item){
       var total = $(this).parents('.goods').find('.price').text();
       sum+=parseFloat(total);
    });
    $('.fr #totalMoney').text(sum);
}
function showCount(){
    var count = $('.cart-list input[type="checkbox"]').length;
    $('.count #totalNum').text(count);
}

function delCart(id) {
//ajax
    obajax.delete(
        '/api/user/cart',
        {id},
        (result) => {
            $('.cart-list input[data-id="' + id + '"]').parents('.goods').remove();
            showCount();
            showSelectedCount();
        },
        (result) => {
            alert(result.msg);
            loadCart();
        }
    );
}

function editCart(id, count) {
    obajax.put(
        '/api/user/cart',
        {id, count},
        (result) => {
            //更新这一行数据
            var cart = $('.cart-list input[data-id="' + id + '"]').parents('.goods');
            cart.find('.unitprice').text(result.data.goods.price);
            cart.find('.count-input').text(result.data.count);
            cart.find('.price').text(result.data.goods.price * result.data.count);
            showSelectedCount();
        },
        (result) => {
            alert(result.msg);
        }
    );
}

//加号事件

function loadCart() {
    obajax.get(
        '/api/user/cart',
        {page: 1, limit: 10},
        (result) => {
            var html = '';
            $.each(result.data.list, (index, cart) => {
                html +=
                    '        <tr class="goods">\n' +
                    '            <td class="first">\n' +
                    '                <div class="check"><input type="checkbox" class="cart1" id="cart-' + cart.id + '" data-id="' + cart.id + '"/></div>\n' +
                    '            </td>\n' +
                    '            <td>\n' +
                    '                <div class="info-box">\n' +
                    '                    <a href="/goods?id=' + cart.goodsId + '"><img src="/upload/' + cart.goods.pics[0].url + '" alt=""></a>\n' +
                    '                    <div class="info">\n' +
                    '                        <a href="/goods?id=' + cart.goodsId + '"><div class="name">' + cart.goods.name + '</div></a>\n' +
                    '                        <div class="meta"><span>' + cart.goods.author + '</span><span>' + cart.goods.press + '</span></div>\n' +
                    '                    </div>\n' +
                    '                </div>\n' +
                    '            </td>\n' +
                    '            <td>￥<span class="unitprice">' + cart.goods.price + '</span></td>\n' +
                    '            <td>\n' +
                    '                <div class="mod-numbox cart-numbox" data-max="' + cart.goods.count + '">\n' +
                    '                    <span class="count-minus"></span>\n' +
                    '                    <span class="count-input">'+cart.count+'</span>\n' +
                    '                    <span class="count-plus"></span>\n' +
                    '                </div>\n' +
                    '            </td>\n' +
                    '            <td class="txt-error">￥<span class="price">' + (cart.goods.price * cart.count) + '</span></td>\n' +
                    '            <td><a href="javascript:;" class="del iconfont icon-shanchu"></a></td>\n' +
                    '        </tr>';
            });
            $('.cart-list').html(html);

            //总共几件商品
            $('#totalNum').text(result.data.list.length);
        }
    )
}